# -*- cording: utf-8 -*-
import threading
from operation.logging import Logging
from PyQt5.QtCore import *

Lock = threading.Lock()


class ReadThreadArduino(QObject):
    Signal_info = pyqtSignal(str)

    def __init__(self):
        super().__init__()

    def run(self, ser):
        Lock.acquire()
        self.read_arduino(ser)
        Lock.release()

    def read_arduino(self, ser):
        try:
            while True:
                n = ser.inWaiting()
                if n:
                    data_buffer = ser.read(n)
                    data = str(data_buffer, encoding='utf-8')
                    self.Signal_info.emit(data)

        except Exception as e:
            Logging.logger.exception(e)
